<?php
/**
 *    @version [ Masterton ]
 *    @package hwdVideoShare
 *    @copyright (C) 2007 - 2009 Highwood Design
 *    @license Creative Commons Attribution-Non-Commercial-No Derivative Works 3.0 Unported Licence
 *    @license http://creativecommons.org/licenses/by-nc-nd/3.0/
 */
defined( '_JEXEC' ) or die( 'Direct Access to this location is not allowed.' );

global $mainframe, $option, $smartyvs;

if (!file_exists(JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_hwdvideoshare'.DS)) {
	echo "<div style=\"border:1px solid #c30;color:#c30;margin: 0 0 5px 0;padding: 5px;font-weight: bold;text-align:left;text-align:center;\">The hwdVideoShare component is not installed, you can not use this module.</div>"; return;
}

$db =& JFactory::getDBO();
$my = & JFactory::getUser();
require_once(JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_hwdvideoshare'.DS.'helpers'.DS.'initialise.php');
hwdvsInitialise::coreRequire();
hwdvsInitialise::language();
hwdvsInitialise::template();
hwdvsInitialise::mysqlQuery();
hwdvsInitialise::definitions();

if (@_HWD_VS_PLUGIN_COMPS !== 214) {
	echo "<div style=\"border:1px solid #c30;color:#c30;margin: 0 0 5px 0;padding: 5px;font-weight: bold;text-align:left;text-align:center;\">This module is not compatible with your version of hwdVideoShare.</div>"; return;
}

$c = hwd_vs_Config::get_instance();
$video_id = JRequest::getInt( 'video_id', null );

/* Security Note: These values are auto-sanitized by mosGetParam() */
$hwdvids_params['tagstyle'] 	    = $params->get( 'tagstyle', '0');
$hwdvids_params['tagtype'] 	        = $params->get( 'tagtype', '0');
$hwdvids_params['textalignment'] 	= $params->get( 'textalignment', 'center');
$hwdvids_params['mod_hwd_itemid']	= (int)$params->get( 'mod_hwd_itemid', '0');
$hwdvids_params['mod_width']	    = $params->get( 'mod_width', '100%');
$hwdvids_params['nottd'] 			= (int)$params->get( 'nottd', '20');
$hwdvids_params['alt_size'] 		= (int)$params->get( 'alt_size', '1');
$hwdvids_params['alt_size_min'] 	= $params->get( 'alt_size_min', '0.6');
$hwdvids_params['alt_size_max'] 	= $params->get( 'alt_size_max', '2.0');
$hwdvids_params['alt_tran'] 		= (int)$params->get( 'alt_tran', '1');
$hwdvids_params['alt_tran_min'] 	= $params->get( 'alt_tran_min', '0.6');
$hwdvids_params['ignorelist'] 	    = $params->get( 'ignorelist', '');

$hwdvids_params['width']            = $params->get( 'width', '100%');
$hwdvids_params['height'] 			= $params->get( 'height', '275');
$hwdvids_params['tcolor'] 			= $params->get( 'tcolor', 'ffffff');
$hwdvids_params['tcolor2'] 			= $params->get( 'tcolor2', 'ffffff');
$hwdvids_params['hicolor'] 			= $params->get( 'hicolor', 'ffffff');
$hwdvids_params['bgcolor'] 			= $params->get( 'bgcolor', 'ffffff');
$hwdvids_params['speed'] 			= $params->get( 'speed', '100');
$hwdvids_params['distr'] 			= $params->get( 'distr', 'true');
$hwdvids_params['mode'] 			= $params->get( 'mode', 'tags');

if ($hwdvids_params['mod_hwd_itemid'] == 0) {
	$hwdvids_params['mod_hwd_itemid'] = hwd_vs_tools::generateValidItemid();
}

if (!function_exists('in_arrayi')) {
	function in_arrayi($needle, $haystack) {
		return in_array(strtolower($needle), array_map('strtolower', $haystack));
	}
}

$ingnorearray = explode(",", $hwdvids_params['ignorelist']);

$join = ' LEFT JOIN #__users AS u ON u.id = a.user_id';
$select = ' a.*, u.name, u.username';
if ($c->cbint == 2) {
	$join.= ' LEFT JOIN #__community_users AS p ON p.userid = a.user_id';
	$select.= ', p.avatar';
} else if ($c->cbint == 1) {
	$join.= ' LEFT JOIN #__comprofiler AS p ON p.id = a.user_id';
	$select.= ', p.avatar';
}

// setup module filter
$where = ' WHERE a.published = 1';
$where .= ' AND a.approved = "yes"';
if (!$my->id) {
	$where .= ' AND a.public_private = "public"';
}

if ($hwdvids_params['tagtype'] == 2) {
	$order = ' ORDER BY rand()';
} else {
	$order = ' ORDER BY a.date_uploaded DESC';
}

if ($hwdvids_params['tagtype'] == 1) {

	$doc =& JFactory::getDocument();
	$meta_title = $doc->getMetaData( "title" );
	$meta_description = $doc->getMetaData( "description" );
	$meta_keywords = $doc->getMetaData( "keywords" );

	$searchterm = $meta_title." ".$meta_description." ".$meta_keywords;
	$searchterm = explode(" ", $searchterm);
	$searchterm = preg_replace("/[^a-zA-Z0-9s_-]/", "", $searchterm);
	$searchterm = implode(" ", $searchterm);

	if (!$my->id) {
		$where = ' WHERE a.public_private = \'public\' AND MATCH (title,tags,description) AGAINST (\''.$searchterm.'\')';
	} else {
		$where = ' WHERE MATCH (title,tags,description) AGAINST (\''.$searchterm.'\')';
	}
	$where.= ' AND a.published = 1';
	$where.= ' AND a.approved = "yes"';

}

	// query filtered video info
	$query = 'SELECT a.tags, a.date_uploaded'
			. ' FROM #__hwdvidsvideos AS a'
			. $where
			. $order
			;
	$db->SetQuery($query);
	$rows = $db->loadObjectList();

	// setup counting variables
	$counter=0;
	$n=count($rows);
	$z = 0;
	$tagcloud = '';
	for ($i=0, $n=count($rows); $i < $n; $i++) {
		$row = $rows[$i];

		$row->tags = stripslashes($row->tags);
		$tags0 = explode(" ", $row->tags);

			$m=count($tags0);
			if ($m > 0) {
				for ($j=0, $m; $j < $m; $j++) {
					$tag0 = $tags0[$j];

					$tag0 = stripslashes($tag0);
					$tags1 = explode(",", $tag0);

					$o=count($tags1);
					$tagList = array();
					if ($o > 0) {
						for ($k=0, $o; $k < $o; $k++) {
							$tag1 = $tags1[$k];

							$tag1 = str_replace(",", "", $tag1);
							$tag1 = str_replace(" ", "", $tag1);
							$tag1 = trim($tag1);
							
							if ($tag1 != ""&&!in_array($tag1,$tagList)) {
								//echo $counter;
								$tagList[]= $tag1;
								$counter++;
								if ($counter>1000) {break;}
								if ($z>$hwdvids_params['nottd']) {break;}
								if (in_arrayi($tag1, $ingnorearray)) {continue;}

								if ($hwdvids_params['alt_size'] == "1") {
									$rand_size = (rand($hwdvids_params['alt_size_min']*100, $hwdvids_params['alt_size_max']*100));
								} else {
									$rand_size = 100;
								}
								if ($hwdvids_params['alt_tran'] == "1") {
									$rand_tran1 = (rand($hwdvids_params['alt_tran_min']*100, 100));
									$rand_tran2 = $rand_tran1/100.00;
								} else {
									$rand_tran1 = 100;
									$rand_tran2 = $rand_tran1/100.00;
								}
								$list[$z]->tag = $tag1;
								$list[$z]->link = 'http://'.$_SERVER['HTTP_HOST'].JRoute::_("index.php?option=com_hwdvideoshare&task=search&Itemid=".$hwdvids_params['mod_hwd_itemid']."&pattern=".$list[$z]->tag);
								$list[$z]->size = $rand_size;
								$list[$z]->transparency1 = $rand_tran1;
								$list[$z]->transparency2 = $rand_tran2;

								if ($hwdvids_params['tagstyle'] == 1) {
									$list[$z]->size = $list[$z]->size/10;
									$tagcloud.= "<a href='".$list[$z]->link."' class='tag-link-".$z."' title='' rel='tag' style='font-size: ".$list[$z]->size."pt;'>".$list[$z]->tag."</a> ";
									$tagcloud = html_entity_decode($tagcloud);
									$smartyvs->assign("print_cloud", 1);
									$smartyvs->assign("cloud_movie", JURI::root().'modules/mod_hwd_vs_sql_tags/tagcloud.swf');
								}

								$z++;
							}
						}
					} else {
						continue;
					}
				}
			} else {
				continue;
			}
		if ($counter>$hwdvids_params['nottd']) {break;}
		$counter=$counter+1;
	}

$smartyvs->assign("list", $list);
$smartyvs->assign("hwdvids_params", $hwdvids_params);
$smartyvs->assign("tagcloud_decoded", $tagcloud);
$smartyvs->assign("tagcloud_encoded", urlencode('<tags>'.$tagcloud.'</tags>'));
$smartyvs->display('mod_hwd_vs_tags.tpl');

return;

?>
